package com.lenovo.newsapp.util;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPool {

	public ThreadPool() {
		
	}
	
	private static int MIN_POOL_SIZE = 2;
	private static int MAX_POOL_SIZE = 10;
	private static int KEEP_ALIVE_TIME = 5000;
	
	private static BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(10);
	
	private static ThreadFactory mThreadFactory = new ThreadFactory() {
		
		private AtomicInteger integer = new AtomicInteger();
		
		@Override
		public Thread newThread(Runnable r) {
			return new Thread(r,"myThreadPool thread:"+integer.getAndIncrement());
		}
	};
	
	private static RejectedExecutionHandler handler = new RejectedExecutionHandler() {
		
		@Override
		public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
			
		}
	};

	private static ThreadPoolExecutor threadPool;
	
	static{
		threadPool = new ThreadPoolExecutor(MIN_POOL_SIZE, MAX_POOL_SIZE,
				KEEP_ALIVE_TIME, TimeUnit.SECONDS, workQueue,mThreadFactory,handler);
	}
	
	public static void execute(Runnable r){
		threadPool.execute(r);
	}
}
